{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a positive integer: 7\n",
      "7=7\n"
     ]
    }
   ],
   "source": [
    "# Python 3\n",
    "n = int(input(\"Enter a positive integer: \"))\n",
    "m, d = n, 2\n",
    "li = []  # 存放所有质因子\n",
    "while d**2 <= m:  # 最多只有一个质因子会大于 sqrt(n)\n",
    "    while m % d == 0:\n",
    "        m //= d  # \"//\" 是整除\n",
    "        li.append(d)\n",
    "    d += 1\n",
    "if m > 1:\n",
    "    li.append(m)  # 大于 sqrt(n) 的质因子\n",
    "if len(li) == 0:\n",
    "    li = [1]\n",
    "print(\"%s=%s\" % (n, \"*\".join([str(x) for x in li])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def factor(n, d = 2):\n",
    "    if n == d:\n",
    "        yield str(n)\n",
    "    elif n % d:\n",
    "        yield str(d)\n",
    "        yield from factor(n, d + 1)\n",
    "    else:\n",
    "        yield from factor(n // d, d)\n",
    "n = int(input(\"Enter a positive integer: \"))\n",
    "if n == 1: print(\"1=1\")\n",
    "else: print(f\"{n}={'*'.join(factor(n))}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
